package top.hundreds.oasys.controller.FrontController;

import org.springframework.web.bind.annotation.*;
import top.hundreds.common.crud.bean.PageParam;
import top.hundreds.common.web.api.ApiResult;
import top.hundreds.oasys.bean.entity.Event;
import top.hundreds.oasys.bean.vo.EventVo;
import top.hundreds.oasys.service.EventService;
import top.hundreds.oasys.utils.MvcHttpUtil;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (Event)表控制层
 *
 * @author lcf
 * @since 2021-01-29 19:15:35
 */
@RestController
@RequestMapping("/front/event")
public class EventController {
    /**
     * 服务对象
     */
    @Resource
    private EventService eventService;

    /**
     * 获取单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/get/{id}")
    public ApiResult<EventVo> selectOne(@PathVariable Long id) {
        return ApiResult.success(this.eventService.buildVo(this.eventService.get(id)));
    }

    /**
     * 流程列表
     *
     * @param pageParam 获取流程列表
     * @return 流程分页
     */
    @PostMapping("/list")
    public ApiResult<List<EventVo>> list(@RequestBody PageParam<Event> pageParam, @RequestAttribute("id") Long userId) {
        Event andQuery;
        if(pageParam.getAndQuery() != null){
            andQuery = pageParam.getAndQuery();
        }else {
            andQuery = new Event();
        }
        andQuery.setUserId(userId);
        pageParam.setAndQuery(andQuery);
        List<EventVo> list = this.eventService.getList(pageParam);
        return ApiResult.success(list);
    }

    /**
     * 未审核流程列表
     *
     * @param pageParam 获取流程列表
     * @return 流程分页
     */
    @PostMapping("/checkList")
    public ApiResult<List<EventVo>> checkList(@RequestBody PageParam<Event> pageParam, @RequestAttribute("id") Long userId) {
        Event andQuery;
        if(pageParam.getAndQuery() != null){
            andQuery = pageParam.getAndQuery();
        }else {
            andQuery = new Event();
        }
        andQuery.setCheckerId(userId);
        andQuery.setStatus(0);
        pageParam.setAndQuery(andQuery);
        List<EventVo> list = this.eventService.getList(pageParam);
        return ApiResult.success(list);
    }

    /**
     * 审核流程列表
     *
     * @param pageParam 分页参数
     * @param userId    用户id
     * @return 已审核流程分页
     */
    @PostMapping("/checkedList")
    public ApiResult<List<EventVo>> checkedList(@RequestBody PageParam<Event> pageParam, @RequestAttribute("id") Long userId) {
        List<Event> checkedList = this.eventService.getCheckedList(pageParam.getPage(), pageParam.getLimit(), userId);
        if (checkedList.size() == 0) {
            return ApiResult.success();
        }
        List<EventVo> eventVoList = new ArrayList<>(checkedList.size());
        for (Event event : checkedList) {
            eventVoList.add(this.eventService.buildVo(event));
        }
        return ApiResult.success(eventVoList);
    }

    /**
     * 新增或编辑流程
     *
     * @param event 流程实体类
     * @return success
     */

    @PostMapping("/save")
    public ApiResult<String> save(@RequestBody Event event) {
        if (event.getId() == null) {
            event.setUserId(MvcHttpUtil.getCurrUserId());
            // 新增操作
            this.eventService.insert(event);
        } else {
            // 更新操作
            this.eventService.update(event);
        }
        return ApiResult.success();
    }

    /**
     * 删除流程
     *
     * @param id 事项id
     * @return success
     */
    @DeleteMapping("/remove/{id}")
    public ApiResult<String> remove(@PathVariable Long id) {
        if (id == null) {
            return ApiResult.fail("请传入参数");
        }
        // 删除操作
        this.eventService.delete(id);
        return ApiResult.success();
    }

    /**
     * 审核通过
     *
     * @param event 流程实体类
     * @return success
     */
    @PostMapping("/checkSuccess")
    public ApiResult<String> complete(@RequestBody Event event) {
        System.out.println(event);
        if (event.getId() == null) {
            return ApiResult.fail("请传入参数");
        }
        this.eventService.setStatus(event.getId(), 1, event.getCheckContent(),new Date());
        return ApiResult.success();
    }

    /**
     * 审核驳回
     *
     * @param event 流程实体类
     * @return success
     */
    @PostMapping("/checkFail")
    public ApiResult<String> unfinished(@RequestBody Event event) {
        if (event.getId() == null) {
            return ApiResult.fail("请传入参数");
        }
        this.eventService.setStatus(event.getId(), 2, event.getCheckContent(),new Date());
        return ApiResult.success();
    }
}
